package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Driver;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IDriverService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yanghao
 * @since 2019-11-16
 */
@Controller
@RequestMapping("/driver")
public class DriverController {

    @Resource
    private IDriverService iDriverService;

    @RequestMapping("{page}")
    public String wel(@PathVariable String page){
        return "logistics/"+ page;
    }

    /*显示所有商品*/
    @ResponseBody
    @RequestMapping("findAll")
    public BlbPage findAllProduct(@RequestParam(required = false, value ="drivername") String drivername, @RequestParam(required = false, value ="driverlicense") String driverlicense, Integer start, Integer length) {
        System.out.println(drivername);
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<Driver> findall = iDriverService.findAll(drivername,companyId, driverlicense, start, length);

        int count = iDriverService.getAllCount(drivername,companyId, driverlicense);


        return  new DataTablePage(count,count,findall);
    }



    @ResponseBody
    @RequestMapping("findalldriver")
    public ResponseHelp findAlldriver(String id){
        List<Driver> list = iDriverService.list(new QueryWrapper<Driver>().eq("logistics_id", id));
        System.out.println(list);
        ResponseHelp responseHelp =new ResponseHelp();
        responseHelp.setData(list);
        return responseHelp;
    }


    @ResponseBody
    @RequestMapping("detelevehicle")
    public ResponseHelp  deteleVehicle(String id){
        iDriverService.removeById(id);
        ResponseHelp responseHelp =new ResponseHelp();
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("findremarks")
    public ResponseHelp findRemarks(String id){
        Driver byId = iDriverService.getById(id);
        String remarks = byId.getRemarks();
        ResponseHelp responseHelp =new ResponseHelp();
        responseHelp.setData(remarks);
        return responseHelp;

    }

    @ResponseBody
    @RequestMapping("updatedriver")
    public ResponseHelp  updateDriver(Driver driver){
        User user = ShiroUtils.getUser();
        String userId = user.getId();
        System.out.println(driver);
        driver.setUpdateId(userId);
        driver.setUpdateTime(new Date());
         iDriverService.updateById(driver);
        ResponseHelp responseHelp =new ResponseHelp();
        return responseHelp;
    }


    @ResponseBody
    @RequestMapping("adddriver")
    public  ResponseHelp  adddRiver(Driver driver){
        User user = ShiroUtils.getUser();
        String userId = user.getId();
        driver.setDriverId(UUIDUtils.getId());
        driver.setCompanyId(userId);
        driver.setCreateTime(new Date());
        driver.setStatus("无任务");
        iDriverService.save(driver);
        ResponseHelp responseHelp =new ResponseHelp();
        return responseHelp;
    }
}

